Tutorial: Writing a custom HIView

D) Implementing support for custom cursor appearance

The support for custom cursor appearance is done by handling the kEventControlTrackingAreaEntered and kEventControlTrackingAreaExited events.

As usual, we add those two events to the event list that we register in GetHICustomViewClass and we add the two very simple cases in the function Internal_HICustomViewHandler:

case kEventControlTrackingAreaEntered:
   {
   // setting the cursor when we pass over our custom view
   SetThemeCursor(kThemeCountingUpHandCursor);
   break;
   }

case kEventControlTrackingAreaExited:
   {
   // resetting the cursor when we leave custom view
   SetThemeCursor(kThemeArrowCursor);
   break;
   }

Of course, if you have multiple parts, you may grab the precise mouse location and key modifiers if any from the EventRef and determine which cursor should be used.

Since we want our custom HIView to have automatic cursor tracking when created, we need to adjust our kEventHIObjectInitialize event handler which now does something useful:

case kEventHIObjectInitialize:
   {
   // always begin kEventHIObjectInitialize by calling through to the previous handler
   status = CallNextEventHandler(inHandlerCallRef, inEvent);
   require_noerr(status, InitializeExit);
   
   // if that succeeded, do our own initialization
   HIViewTrackingAreaRef trackRef;
   status = HIViewNewTrackingArea(myData->view, NULL, 0, &trackRef);
   require_noerr(status, InitializeExit);

InitializeExit:
   break;
   }

We do not need to clean after ourselves in the kEventHIObjectDestruct event handler since the HIToolbox will automatically dispose any tracking area associated with your view when that view is destroyed.

In our example you will simply see:

You can test the custom HIView for this step by using the project located in the folder “5_Tracking_Area”.


Next Page

Previous Page

Return to Main Page